package xyz.weblade.lynchmybatis.utils.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 假设 MultiExcelNode 注解已经定义
@Retention(RetentionPolicy.RUNTIME)
@interface MultiExcelNode {}

class TreeNode {
    private Object data;
    private List<TreeNode> children;

    public TreeNode(Object data) {
        this.data = data;
        this.children = new ArrayList<>();
    }

    public void addChild(TreeNode child) {
        children.add(child);
    }

    public Object getData() {
        return data;
    }

    public List<TreeNode> getChildren() {
        return children;
    }
}

class CountTool{
    // 用于缓存每个节点的叶子节点数量
    private final Map<TreeNode, Integer> leafCountCache = new HashMap<>();

    // 递归计算节点的叶子节点数量并缓存结果
    public  int countLeaves(TreeNode node) {
        if (leafCountCache.containsKey(node)) {
            return leafCountCache.get(node);
        }
        if (node.getChildren().isEmpty()) {
            leafCountCache.put(node, 1);
            return 1;
        }
        int totalLeaves = 0;
        for (TreeNode child : node.getChildren()) {
            totalLeaves += countLeaves(child);
        }
        leafCountCache.put(node, totalLeaves);
        return totalLeaves;
    }

    public Map<TreeNode, Integer> getLeafCountCache(){
        return leafCountCache;
    }
}
